@model VotingSystem.ViewModels.VoteEditViewModel

@{
    ViewData["Title"] = "编辑投票";
}

<div class="container">
    <div class="row mb-4">
        <div class="col">
            <h1 class="display-4">编辑投票</h1>
        </div>
    </div>

    <div class="row">
        <div class="col-md-8">
            <div class="card shadow-sm">
                <div class="card-body">
                    <form asp-action="Edit" method="post">
                        <input type="hidden" asp-for="Id" />
                        <div asp-validation-summary="ModelOnly" class="text-danger"></div>

                        <div class="mb-3">
                            <label asp-for="Title" class="form-label"></label>
                            <input asp-for="Title" class="form-control" />
                            <span asp-validation-for="Title" class="text-danger"></span>
                        </div>

                        <div class="mb-3">
                            <label asp-for="Description" class="form-label"></label>
                            <textarea asp-for="Description" class="form-control" rows="3"></textarea>
                            <span asp-validation-for="Description" class="text-danger"></span>
                        </div>

                        <div class="mb-3">
                            <label asp-for="EndTime" class="form-label"></label>
                            <input asp-for="EndTime" class="form-control" type="datetime-local" />
                            <span asp-validation-for="EndTime" class="text-danger"></span>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <div class="form-check">
                                    <input asp-for="IsPublic" class="form-check-input" />
                                    <label asp-for="IsPublic" class="form-check-label"></label>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-check">
                                    <input asp-for="IsAnonymous" class="form-check-input" />
                                    <label asp-for="IsAnonymous" class="form-check-label"></label>
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <div class="form-check">
                                    <input asp-for="RequireLogin" class="form-check-input" />
                                    <label asp-for="RequireLogin" class="form-check-label"></label>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-check">
                                    <input asp-for="AllowModify" class="form-check-input" />
                                    <label asp-for="AllowModify" class="form-check-label"></label>
                                </div>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label asp-for="Type" class="form-label"></label>
                                <select asp-for="Type" class="form-select" asp-items="Html.GetEnumSelectList<VoteType>()"></select>
                                <span asp-validation-for="Type" class="text-danger"></span>
                            </div>
                            <div class="col-md-6">
                                <label asp-for="MaxVotesPerUser" class="form-label"></label>
                                <input asp-for="MaxVotesPerUser" class="form-control" type="number" min="1" max="10" />
                                <span asp-validation-for="MaxVotesPerUser" class="text-danger"></span>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label asp-for="ResultVisibility" class="form-label"></label>
                                <select asp-for="ResultVisibility" class="form-select" asp-items="Html.GetEnumSelectList<ResultVisibility>()"></select>
                                <span asp-validation-for="ResultVisibility" class="text-danger"></span>
                            </div>
                            <div class="col-md-6">
                                <label asp-for="Password" class="form-label"></label>
                                <input asp-for="Password" class="form-control" />
                                <span asp-validation-for="Password" class="text-danger"></span>
                            </div>
                        </div>

                        <div class="mb-3">
                            <div class="form-check">
                                <input asp-for="ShowResultsAfterEnd" class="form-check-input" />
                                <label asp-for="ShowResultsAfterEnd" class="form-check-label"></label>
                            </div>
                        </div>

                        <div class="mb-4">
                            <h4>选项</h4>
                            <div id="optionsContainer">
                                @for (var i = 0; i < Model.Options.Count; i++)
                                {
                                    <div class="card mb-3 option-card">
                                        <div class="card-body">
                                            <input type="hidden" asp-for="Options[i].Id" />
                                            <div class="mb-3">
                                                <label asp-for="Options[i].Title" class="form-label"></label>
                                                <input asp-for="Options[i].Title" class="form-control" />
                                                <span asp-validation-for="Options[i].Title" class="text-danger"></span>
                                            </div>
                                            <div class="mb-3">
                                                <label asp-for="Options[i].Description" class="form-label"></label>
                                                <textarea asp-for="Options[i].Description" class="form-control" rows="2"></textarea>
                                                <span asp-validation-for="Options[i].Description" class="text-danger"></span>
                                            </div>
                                            <div class="mb-3">
                                                <label asp-for="Options[i].ImageUrl" class="form-label"></label>
                                                <input asp-for="Options[i].ImageUrl" class="form-control" />
                                                <span asp-validation-for="Options[i].ImageUrl" class="text-danger"></span>
                                            </div>
                                            <button type="button" class="btn btn-danger btn-sm remove-option">
                                                <i class="fas fa-trash"></i> 删除选项
                                            </button>
                                        </div>
                                    </div>
                                }
                            </div>
                            <button type="button" class="btn btn-outline-primary" id="addOption">
                                <i class="fas fa-plus"></i> 添加选项
                            </button>
                        </div>

                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> 保存修改
                            </button>
                            <a asp-action="Details" asp-route-id="@Model.Id" class="btn btn-outline-secondary">
                                <i class="fas fa-times"></i> 取消
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script>
        $(document).ready(function() {
            // 添加选项
            $('#addOption').click(function() {
                var index = $('.option-card').length;
                var template = `
                    <div class="card mb-3 option-card">
                        <div class="card-body">
                            <div class="mb-3">
                                <label class="form-label">选项标题</label>
                                <input name="Options[${index}].Title" class="form-control" required />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">选项描述</label>
                                <textarea name="Options[${index}].Description" class="form-control" rows="2"></textarea>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">选项图片</label>
                                <input name="Options[${index}].ImageUrl" class="form-control" />
                            </div>
                            <button type="button" class="btn btn-danger btn-sm remove-option">
                                <i class="fas fa-trash"></i> 删除选项
                            </button>
                        </div>
                    </div>
                `;
                $('#optionsContainer').append(template);
            });

            // 删除选项
            $(document).on('click', '.remove-option', function() {
                if ($('.option-card').length > 2) {
                    $(this).closest('.option-card').remove();
                } else {
                    alert('至少需要保留两个选项！');
                }
            });
        });
    </script>
} 